package edu.sctu.lab03;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Adminitrator on 2018/5/28.
 */
public class Database {

    public List<User> getUserList()  {
        List<User> userList = new ArrayList<User>();

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        String sql = "select * from t_users";
        //加载驱动程序\
        try {
            Class.forName("com.mysql.jdbc.Driver");

            connection = DriverManager.getConnection(
                    "jdbc:mysql://47.104.80.155:3306/javaweb",
                    "javaweb",
                    "JAVAweb123!@#");
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String password = resultSet.getString(3);
                User user = new User();
                user.setId(id);
                user.setName(name);
                user.setPassword(password);

                userList.add(user);

            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }



}
